﻿namespace Resharper.Abstractions.CodeDom.Statements
{
  using System;
  using JetBrains.Annotations;
  using JetBrains.ReSharper.Feature.Services.LinqTools;
  using JetBrains.ReSharper.Psi.Tree;
  using Resharper.Abstractions.CodeDom.Expressions;
  using Resharper.Abstractions.CodeDom.TreeNodes;

  /// <summary>Defines the <see cref="Statement"/> class.</summary>
  public class Statement : TreeNode
  {
    #region Constructors and Destructors

    /// <summary>Initializes a new instance of the <see cref="Expression"/> class.</summary>
    /// <param name="statement">The expression.</param>
    internal Statement([NotNull] IStatement statement) : base(statement)
    {
    }

    #endregion

    #region Properties

    /// <summary>The internal statement field.</summary>
    [NotNull]
    protected internal IStatement InternalStatement
    {
      get
      {
        return (IStatement)this.InternalTreeNode;
      }
    }

    #endregion

    #region Public Methods

    /// <summary>Replaces the by.</summary>
    /// <param name="statement">The statement.</param>
    /// <returns>Returns the by.</returns>
    [CanBeNull]
    public Statement ReplaceBy([NotNull] Statement statement)
    {
      if (statement == null)
      {
        throw new ArgumentNullException("statement");
      }

      var result = this.InternalStatement.ReplaceBy(statement.InternalStatement);
      
      return result != null ? StatementFactory.GetStatement(result) : null;
    }

    #endregion
  }
}